package services;

import java.util.ArrayList;
import java.util.List;

import business.SendValidationResult;
import business.ValidationError;
import business.ValidationReason;
import dao.Filter;
import dao.ObjectDao;
import exceptions.ExDBException;

public class ValidationService {
	
	public static final Long WARNING_TYPE_ID = 1L;
	public static final Long ERROR_TYPE_ID = 2L;
	
	public List<ValidationError> getValidationErrorsByCompanyCampaignIDOrderByErrors(Long companyCampaignID)
	{
		ObjectDao dao= new ObjectDao();
		List<Filter> filters = new ArrayList<Filter>();
		Filter filter = new Filter(Filter.NUMERIC_FILTER, "EE.ID_EMP_CAMP", Filter.EQUAL_OPERATOR,companyCampaignID,true);
		filters.add(filter);
		String orderClause = "ORDER BY V.ID_TIPO_VALIDACION DESC,VE.ID_VALIDACION_ERROR ASC";
		return dao.getValidationErrors(filters, ObjectDao.AND_OPERATOR,orderClause);
	}
	
	public List<ValidationError> getValidationErrorsByCompanyCampaignIDOrderByWarnings(Long companyCampaignID)
	{
		ObjectDao dao= new ObjectDao();
		List<Filter> filters = new ArrayList<Filter>();
		Filter filter = new Filter(Filter.NUMERIC_FILTER, "EE.ID_EMP_CAMP", Filter.EQUAL_OPERATOR,companyCampaignID,true);
		filters.add(filter);
		String orderClause = "ORDER BY V.ID_TIPO_VALIDACION ASC,VE.ID_VALIDACION_ERROR ASC";
		return dao.getValidationErrors(filters, ObjectDao.AND_OPERATOR,orderClause);
	}
	
	public SendValidationResult validateSendValidation(Long companyCampaignID)
	{
		SendValidationResult sendValidationResult = new SendValidationResult();
		ObjectDao dao= new ObjectDao();
		List<Filter> filters = new ArrayList<Filter>();
		
		//Traigo las validaciones tipo Warning sin comentarios
		Filter filter = new Filter(Filter.STRING_FILTER, "VE.ID_ENC_EMP", Filter.EQUAL_OPERATOR,companyCampaignID,true);
		filters.add(filter);
		filter = new Filter(Filter.NUMERIC_FILTER, "V.ID_TIPO_VALIDACION", Filter.EQUAL_OPERATOR,WARNING_TYPE_ID,true);
		filters.add(filter);
		//Filtro que observaciones tenga algun texto
		filter = new Filter(" (observaciones is null or length(trim(observaciones)) is null)");
		filters.add(filter);
		List<ValidationError> warningValidations = dao.getValidationErrors(filters, ObjectDao.AND_OPERATOR,null);
		if(warningValidations != null)
		{
			sendValidationResult.setWarningsCount(warningValidations.size());
		}
		else
		{
			sendValidationResult.setWarningsCount(0);
		}
		//Traigo las validaciones tipo ERROR
		filters = new ArrayList<Filter>();		
		filter = new Filter(Filter.STRING_FILTER, "VE.ID_ENC_EMP", Filter.EQUAL_OPERATOR,companyCampaignID,true);
		filters.add(filter);
		filter = new Filter(Filter.NUMERIC_FILTER, "V.ID_TIPO_VALIDACION", Filter.EQUAL_OPERATOR,ERROR_TYPE_ID,true);
		filters.add(filter);
		List<ValidationError> errorValidations = dao.getValidationErrors(filters, ObjectDao.AND_OPERATOR,null);
		if(errorValidations != null)
		{
			sendValidationResult.setErrorsCount(errorValidations.size());
		}
		else
		{
			sendValidationResult.setErrorsCount(0);
		}
		//Si no tengo validaciones incorrectas(Errores o Advertencias sin comentarios) seteo valid en true, si no pongo false
		if(sendValidationResult.getErrorsCount() == 0 && sendValidationResult.getWarningsCount() == 0)
		{
			sendValidationResult.setValid(true);
		}
		else
		{
			sendValidationResult.setValid(false);
		}		
		return sendValidationResult;
	}
		
	public void saveValidationErrorByID(Long validationErrorID,Long reasonID,String observation) throws ExDBException
	{
		ObjectDao dao = new ObjectDao();
		dao.updateValidationError(validationErrorID,reasonID,observation);
	}

	public void validatePollsByCompanyCampaign(Long companyCampaignID) throws ExDBException
	{
		ObjectDao dao = new ObjectDao();
		dao.validatePolls(companyCampaignID);
	}
	
	public List<ValidationReason> getValidationReasonsByValidationID(Long validationID)
	{
		ObjectDao dao= new ObjectDao();
		List<Filter> filters = new ArrayList<Filter>();
		Filter filter = new Filter(Filter.NUMERIC_FILTER, "VM.ID_VALIDACION", Filter.EQUAL_OPERATOR,validationID,true);
		filters.add(filter);
		return dao.getValidationReasons(filters, ObjectDao.AND_OPERATOR);
	}
	
	public SendValidationResult sendPollsByCompanyCampaign(Long companyCampaignID) throws ExDBException
	{
		ObjectDao dao = new ObjectDao();
		return dao.sendPolls(companyCampaignID);
	}
}
